package edu.nus.iss.t5.jass.dao;

import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;



import edu.nus.iss.t5.jass.entity.Entity;
import edu.nus.iss.t5.jass.entity.JobAdvertisement;
import edu.nus.iss.t5.jass.entity.JobResponse;
import edu.nus.iss.t5.jass.util.DateUtil;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

/**
 * 
 * ResponseDAO implements getResponsesByAdvert and getJobResponse from
 * Response.properties file.
 * 
 * @author Modified by Abdul Khader Azeem 
 */
public class JobResponseDAO {

    private static String responcePropFile = "data\\response.properties";
    private static String key = "id.";
    private final String jobseekerID = "jobseekerid.";
    private final String advertisementID = "advertisementid.";
    private final String responseDate = "responsedate.";
    private final String cv = "cv.";
    private Properties props;
    private Hashtable<String, JobResponse> jobResponseList;
    private JobSeekerDAO seekerDAO;

    /**
     * Constructor for JobResponseDAO, this constructor will load Response.properties file.
     * @throws IOException if the system cannot find response.properties, the exception will be thrown.
     */
    public JobResponseDAO() throws IOException {
        props = new Properties();

        File f = new File(responcePropFile);
        if (f.exists()) {

            FileInputStream in = new FileInputStream(f);

            props.load(in);
            in.close();
            jobResponseList = new Hashtable<String, JobResponse>();

        } else {
            throw new IOException("The system cannot find file: " + this.responcePropFile);
        }
        seekerDAO=new JobSeekerDAO();
    
    }

    private void getAll() {
        if (jobResponseList == null) {
            jobResponseList = new Hashtable<String, JobResponse>();
        } else {
            jobResponseList.clear();
        }

        Set<Object> keys = props.keySet();
        Iterator itr = keys.iterator();
        String tempKey="";
        String id = "";
        while (itr.hasNext()) {
            tempKey = (String) itr.next();
            if (tempKey.startsWith(key)) {
                JobResponse resp = new JobResponse();
                resp.setId(props.getProperty(tempKey));
                id = resp.getId();
                resp.setResume(props.getProperty(this.cv + id));

                try {
                    String seekerId = props.getProperty(this.jobseekerID + id);
                    resp.setJobSeeker(seekerDAO.getJobSeeker(seekerId));

                    String advId = props.getProperty(this.advertisementID + id);
                    JobAdvertisement adv=new JobAdvertisement();
                    adv.setId(advId);
                    resp.setJobAdvertisement(adv);

                    resp.setResponseDate(DateUtil.convertStrToDate(props.getProperty(this.responseDate + id)));
                } catch (ParseException ex) {
                    ex.printStackTrace();
                }
                jobResponseList.put(id, resp);
            }

        }
    }

    /**
     * Get list of Job Response by given Job Advertisement entity object.
     * @param jobAdvertisement the Job Advertisement entity object.
     * @return List of Job Responser which response to the given Job Advertisement.
     */
    public List<JobResponse> getResponsesByAdvert(JobAdvertisement jobAdvertisement) {
        if (jobResponseList == null || jobResponseList.isEmpty()) {
            this.getAll();
            ;
        }
        List<JobResponse> responseList = new ArrayList<JobResponse>();
        Enumeration<JobResponse> enumeration = jobResponseList.elements();
        while (enumeration.hasMoreElements()) {
            JobResponse response = enumeration.nextElement();
            if (response.getJobAdvertisement().getId().equals(jobAdvertisement.getId())) {
                response.setJobAdvertisement(jobAdvertisement);
                responseList.add(response);
            }
        }

        return responseList;
    }

    /**
     * Get Job Response object by given response ID
     * @param responseID unique identity of Job Response Object.
     * @return The Job Response entity object.
     * return null if cannot find Job Response by given ID.
     */
    public JobResponse getJobResponse(String responseID) {
        if (jobResponseList == null || jobResponseList.isEmpty()) {
            this.getAll();
            ;
        }
        JobResponse jobResponse = jobResponseList.get(responseID);

        return jobResponse;
    }
}
